package com.woniu.service.Impl.homePage;

import com.aliyuncs.utils.StringUtils;
import com.woniu.dao.homePage.OrderDao;
import com.woniu.dao.homePage.UsersInfoDao;
import com.woniu.entity.UsersInfo;
import com.woniu.service.homePage.IUsersInfoService;
import com.woniu.util.ResData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 个人资料实现类
 * @author 李松桦
 * @data 2023/3/5 15:09
 */
@Service
public class UsersInfoServiceImpl implements IUsersInfoService {
    @Autowired
    UsersInfoDao usersInfoDao;

    @Autowired
    OrderDao orderDao;
    /*根据 手机号展示个人资料信息*/
    @Override
    public ResData<?> getUserInfoByPhone(String phone) {
        //用户未登录，返回未登录
        if (phone== null || "null".equals(phone) || "".equals(phone)){
            return ResData.NOT_LOGIN;
        }
        UsersInfo usersInfo = usersInfoDao.getUserInfoByPhone(phone);
        return new ResData<>().ok(usersInfo);
    }

    /*编辑用户个人资料*/
    @Override
    public ResData<?> updateUserInfo(UsersInfo usersInfo) {
        usersInfoDao.updateUserInfo(usersInfo);
        return new ResData<>().ok();
    }

    /*查询用户的积分和会员等级*/
    @Override
    public  ResData<?> queryUserInfoCreditAndLevel(String phone) {
        //用户未登录，返回未登录
        if (phone== null || "null".equals(phone) || "".equals(phone)){
            return ResData.NOT_LOGIN;
        }
        //查询出用户资料表的用户积分
        UsersInfo creditAndLevel = usersInfoDao.queryUserInfoCreditAndLevel(phone);
        Integer credit = creditAndLevel.getCredit();
        Integer levels = creditAndLevel.getLevels();
        //查询出订单表该用户所有订单的总金额
        Double ordersAmountByPhone = orderDao.getOrdersAmountByPhone(phone);
        Integer amount=ordersAmountByPhone.intValue();
        //如果一致,返回
        if (credit == amount){
             return new ResData<>().ok(creditAndLevel);
             //不一致
    }  else {
            //积分小于1000，会员等级0
            if (amount < 1000){
                levels=0;
                //积分大于1000，小于5000.会员等级1
            } else if (amount >= 1000 && amount <5000) {
                levels=1;
                //积分大于5000，小于10000.会员等级2
            } else if (amount >= 5000 && amount <10000) {
                levels=2;
                //积分大于10000，小于100000.会员等级3
            } else if (amount>= 10000 && amount <100000) {
                levels=3;
                //积分大于100000，小于1000000.会员等级4
            } else if (amount>= 100000 && amount <1000000) {
                levels=4;
                //积分大于1000000，会员等级5
            } else if (amount>= 1000000 ) {
                levels=5;
            }
            //修改用户资料表的积分改成和订单表用户总金额 会员等级一致，
            usersInfoDao.updateUserInfoCredit(amount,levels,phone);
            UsersInfo usersInfo =   usersInfoDao.queryUserInfoCreditAndLevel(phone);
            return new ResData<>().ok(usersInfo);
        }
    }

    @Override
    public ResData<?> queryUsersLevel(String phone) {
        UsersInfo u =usersInfoDao.queryUsersLevel(phone);
        return new ResData<>().ok(u);
    }
}
